import {Component, OnInit,TemplateRef, OnDestroy} from "@angular/core";
import {NormalService} from "../../../apiServices/normal.service";
import {Subscription} from "rxjs/Subscription";
import {Router} from '@angular/router';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import {FormControl, Validators} from "@angular/forms";

declare let window: any;

@Component({
    selector: 'transfer',
    templateUrl: './transfer.template.html'
})

export class TransferComponent implements OnInit, OnDestroy{
    public language: string = 'en';
    private userSubscription: Subscription;
    public user: any={};
    public userModel: any = {};
    public transferModel: any = {
        toUserEmail: '',
        amount: '',
        paypassword: ''
    }
    private checkVerifyOnce: boolean;
    checkMessage: number;
    modalRef: BsModalRef;
    message: string;
    isVerified: any = {en: 'you have not verified yet !', zh: '您还未通过实名认证！'};
    transferSucc: any = {en: 'transfer successful,current account balance is:', zh: '转账成功，当前账户余额为：'}

    constructor( private  normalService: NormalService, private modalService: BsModalService, private router: Router){
        this.language = normalService.lang;
        this.userSubscription = normalService.user$.subscribe(user=>{
            if (user.verified == false){
                if(!this.checkVerifyOnce){
                    this.checkVerifyOnce = !this.checkVerifyOnce;
                    if(this.normalService.lang == 'en'){
                        alert(this.isVerified.en);
                    }
                    if(this.normalService.lang == 'zh'){
                        alert(this.isVerified.zh);
                    }
                }
                router.navigate(['/account']);
            }
            this.user = user;
        });
    }

    control = new FormControl('', Validators.max(15));

    checkMoney(){
        if(this.transferModel.amount <= 0){
            this.checkMessage = 1;
        }else if(this.transferModel.amount > this.user.account.balance){
            this.checkMessage = 2;
        }else {
            this.checkMessage = 3;
        }
    }

    openModal(template: TemplateRef<any>) {
        this.modalRef = this.modalService.show(template);
    }

    confirm(): void {
        this.userModel.email = this.user.email;
        // console.log("transferModel",this.transferModel);
           
            this.normalService.transforToken(this.transferModel).then(user=>{
                if(this.normalService.lang == 'en'){
                    alert(this.transferSucc.en + user.account.balance);
                }
                if(this.normalService.lang == 'zh'){
                    alert(this.transferSucc.zh + user.account.balance);
                }
            }).catch((error) => {
                alert(this.normalService.getErrors(error));
            })
            
            this.message = 'Confirmed!';
        this.modalRef.hide();
    }

    checkTransferHistory(): void {
        this.router.navigate(['/account/transferHistory']);
    }

    decline(): void {
        this.message = 'Declined!';
        this.modalRef.hide();
    }
    
    ngOnInit(): void {
        this.normalService.getuser();
    }

    ngOnDestroy(): void {
        this.userSubscription.unsubscribe();
    }
}